package prehilife;

import java.util.Iterator;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PrehiWorld extends AbstractWorld {

    private int currentIndex;
    private int nbMammouths;
    private int nbMembers;
    private int nbCaves;
    private int nbChief;
    private int nbHuts;
    public int maxHP;
    public int collapseChance;
    public int turnInCave;
    public FactoryPrehiCreature factory;
    private SimuLife simuLife;
    private Random deplRand = new Random();
    private boolean auto;

    public void configPrehiWorld(SimuLife simuLife, int nbMammouths, int nbMembers,
            int nbCaves, int nbHuts, int maxHP, int collapseChance, int turnInCave,
            boolean auto, boolean graphicMode) {
        this.simuLife = simuLife;
        this.nbMammouths = nbMammouths;
        this.nbMembers = nbMembers;
        this.nbCaves = nbCaves;
        this.nbHuts = nbHuts;
        this.maxHP = maxHP;
        this.collapseChance = collapseChance;
        this.turnInCave = turnInCave;
        this.auto = auto;
        factory = new FactoryPrehiCreature(this);

        // create Mammouth
        for (int i = 0; i < nbMammouths; i++) {
            Mammouth m = factory.createMammouth();
            myGameBoard[m.getPositionX()][m.getPositionY()].add(m);
        }
        // create Members
        for (int i = 0; i < nbMembers; i++) {
            Member m = factory.createMember();
            myGameBoard[m.getPositionX()][m.getPositionY()].add(m);
            if (i == 0) {
                m.setChief(true);
                nbChief++;
            }
        }
        // create Caves
        for (int i = 0; i < nbCaves; i++) {
            Cave m = factory.createCave();
            myGameBoard[m.getPositionX()][m.getPositionY()].add(m);
        }
        // create Huts
        for (int i = 0; i < nbHuts; i++) {
            Hut m = factory.createHut();
            myGameBoard[m.getPositionX()][m.getPositionY()].add(m);
        }
    }

    @Override
    public Creature getNextCreature() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void moveCreature(Creature cr) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void displayCaseImage(int x, int y) {
    }

    @Override
    public void displayCaseText() {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean finished() {
        throw new UnsupportedOperationException();
    }

    public int getNbCaves() {
        return nbCaves;
    }

    public void setNbCaves(int nbCaves) {
        this.nbCaves = nbCaves;
    }

    public int getNbMammouths() {
        return nbMammouths;
    }

    public void setNbMammouths(int nbMammouths) {
        this.nbMammouths = nbMammouths;
    }

    public int getNbMembers() {
        return nbMembers;
    }

    public void setNbMembers(int nbMembers) {
        this.nbMembers = nbMembers;
    }

    public void createMembre(int posX, int posY) {
        Member m = factory.createMember(posX, posY);
        myGameBoard[m.getPositionX()][m.getPositionY()].add(m);
        setNbMembers(getNbMembers() + 1);
    }

    public int getNbChief() {
        return nbChief;
    }

    public void setNbChief(int nbChief) {
        this.nbChief = nbChief;
    }

    public String runGame() {
        while (nbMammouths > 0 && nbMembers > 0) {
            for (int i = 0; i < myGameBoard.length; i++) {
                for (int j = 0; j < myGameBoard[i].length; j++) {
                    Iterator<Creature> creatureItr = myGameBoard[i][j].iterator();
                    while (creatureItr.hasNext()) {
                        Creature cre = creatureItr.next();
                        if (cre instanceof MobileCreature) {
                            int deplX = deplRand.nextInt(super.getSizeX() / 10 + 2);
                            int deplY = deplRand.nextInt(super.getSizeY() / 10 + 2);
                            simuLife.queue.add(new Move((MobileCreature) cre, deplX, deplY));
                            if (cre instanceof Member) {
                                simuLife.queue.add(new Evolve((Member) cre));
                            }
                        } else if (cre instanceof Hut) {
                            Hut h = (Hut) cre;
                            h.healMembers();
                        } else if (cre instanceof Cave) {
                            Cave c = (Cave) cre;
                            simuLife.queue.add(new Evolve(c));
                        }
                    }
                }
            }
            simuLife.executeAll();
            if (auto) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(PrehiWorld.class.getName()).log(Level.SEVERE, null, ex);
                }
            }else{
                theGUI.isBlocked();
            }
            super.display();
        }
        super.display();
        return "nbChief:" + nbChief + " nbmembers: " + nbMembers + "    nbMammouths:" + nbMammouths;
    }
}